{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e6c376af",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_key = 'iE1MiracWI1O1jmSAZWUrhdS7FOvV8My'\n",
    "API_secret = '8B16HiL0padJMA1cBvkYCU0lPJ8hqZ_l'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e1088f7",
   "metadata": {},
   "source": [
    "## 创建人脸集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2fd08b35",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 96,\n",
       " 'error_message': 'FACESET_EXIST',\n",
       " 'request_id': '1649010743,47646186-5a61-448c-9697-f9bcb6c1a68c'}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "create_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "payload = {\n",
    "    \"api_key\": API_key,\n",
    "    \"api_secret\": API_secret,\n",
    "    \"display_name\": \"快乐家庭\",\n",
    "    \"outer_id\": \"1-405\",\n",
    "}\n",
    "r = requests.post(create_url, params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0114e1dd",
   "metadata": {},
   "source": [
    "## 查询人脸库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1d18ad5d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 80,\n",
       " 'facesets': [{'faceset_token': 'fceee5436e64c826dee8484d1f4f33cd',\n",
       "   'outer_id': 'API0102',\n",
       "   'display_name': '新闻网新党支部 64人 48女',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'a8c850bbaef527a32a7a9064cbd4836b',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '6e2b4b96a5ccfe643dc02d416a2d6ed5',\n",
       "   'outer_id': '1-405',\n",
       "   'display_name': '快乐家庭',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1649010761,fcb7e4cd-b406-47ed-a2ca-307a81c35ec0'}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "getfacesets_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets\"\n",
    "payload = {\n",
    "    \"api_key\": API_key,\n",
    "    \"api_secret\": API_secret,\n",
    "}\n",
    "r = requests.post(url = getfacesets_url, params = payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7c46a84a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'6e2b4b96a5ccfe643dc02d416a2d6ed5'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "faceset_token = r.json()[\"facesets\"][-1][\"faceset_token\"]\n",
    "faceset_token"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0ff005f",
   "metadata": {},
   "source": [
    "## 人脸检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ab3dc632",
   "metadata": {},
   "outputs": [],
   "source": [
    "import detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3e1bea9a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1649010797,72ccb3da-2448-4d15-8122-6623b1f79d75',\n",
       " 'time_used': 120,\n",
       " 'faces': [{'face_token': 'e5de600c13521d8fffb6b1a5dcbd3774',\n",
       "   'face_rectangle': {'top': 114, 'left': 550, 'width': 142, 'height': 142}}],\n",
       " 'image_id': 'PlWrpfV5TSzGiVZUC5ci4w==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第一张\n",
    "detect.face_detect(API_key,API_secret,\"1.jpeg\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "fdc02b88",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'79413f8d5516723c2aa44e1672ad9161'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first = detect.face_detect(API_key,API_secret,\"1.jpeg\")\n",
    "face_token1 = first[\"faces\"][0][\"face_token\"]\n",
    "face_token1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "995699d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import addface"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "6e36a9a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '6e2b4b96a5ccfe643dc02d416a2d6ed5',\n",
       " 'time_used': 481,\n",
       " 'face_count': 3,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1649010952,ded68b78-27c0-4617-b5ed-d2379a4ef39d',\n",
       " 'outer_id': '1-405',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# token1加入集合\n",
    "addface.face_addface(API_key,API_secret,faceset_token, face_token1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a3ca06c",
   "metadata": {},
   "source": [
    "## 人脸分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ec825db0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import analyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c8550437",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 174,\n",
       " 'request_id': '1649011033,6d4dde46-872d-4e3b-95c5-550cb3c85a67',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 0.539,\n",
       "     'neutral': 79.689,\n",
       "     'disgust': 0.209,\n",
       "     'anger': 0.033,\n",
       "     'surprise': 15.146,\n",
       "     'fear': 2.18,\n",
       "     'happiness': 2.204},\n",
       "    'beauty': {'female_score': 80.449, 'male_score': 78.226},\n",
       "    'gender': {'value': 'Female'},\n",
       "    'age': {'value': 23},\n",
       "    'mouthstatus': {'close': 100.0,\n",
       "     'surgical_mask_or_respirator': 0.0,\n",
       "     'open': 0.0,\n",
       "     'other_occlusion': 0.0},\n",
       "    'glass': {'value': 'None'},\n",
       "    'skinstatus': {'dark_circle': 2.022,\n",
       "     'stain': 5.775,\n",
       "     'acne': 3.051,\n",
       "     'health': 67.891},\n",
       "    'headpose': {'yaw_angle': 28.225603,\n",
       "     'pitch_angle': -2.8796897,\n",
       "     'roll_angle': 15.29833},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 0.098},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 0.098},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 0.098}},\n",
       "    'smile': {'threshold': 50.0, 'value': 78.114},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.065,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.029,\n",
       "      'no_glass_eye_open': 99.883,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.022},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 0.006,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 99.994,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 0.043},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.571,\n",
       "      'vector_z_component': 0.934,\n",
       "      'vector_x_component': 0.348,\n",
       "      'vector_y_component': 0.085,\n",
       "      'position_y_coordinate': 0.447},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.578,\n",
       "      'vector_z_component': 0.926,\n",
       "      'vector_x_component': 0.342,\n",
       "      'vector_y_component': 0.159,\n",
       "      'position_y_coordinate': 0.461}}},\n",
       "   'face_rectangle': {'width': 142, 'top': 114, 'left': 550, 'height': 142},\n",
       "   'face_token': '79413f8d5516723c2aa44e1672ad9161'}]}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "analyze.face_analyze(API_key,API_secret,face_token1,\"gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d72805c",
   "metadata": {},
   "source": [
    "## 人脸搜索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "38f7f5be",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1b5dbe48465b960e16e9f8e85df4fe74'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第二张\n",
    "second = detect.face_detect(API_key,API_secret,\"2.jpeg\")\n",
    "face_token2 = second[\"faces\"][0][\"face_token\"]\n",
    "face_token2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d05f7f43",
   "metadata": {},
   "outputs": [],
   "source": [
    "import search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "dd78ff65",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1649011197,14f115fe-1813-4ad1-a214-5ed2b82d4aca',\n",
       " 'time_used': 365,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'results': [{'confidence': 60.204,\n",
       "   'user_id': '',\n",
       "   'face_token': '91b4437c1dc6990d493c0834a1af8da9'}]}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 搜索\n",
    "search.face_search(API_key,API_secret,face_token2,faceset_token)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca9e6c2b",
   "metadata": {},
   "source": [
    "## 人脸比对"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "5438971b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'8e49f20c9068d9b5849247307a08d14d'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第三张\n",
    "three = detect.face_detect(API_key,API_secret,\"3.jpeg\")\n",
    "face_token3 = three[\"faces\"][0][\"face_token\"]\n",
    "face_token3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "4521b9cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'4366d0f9b633e8d744ae4bb51a3ba360'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第四张\n",
    "four = detect.face_detect(API_key,API_secret,\"4.jpeg\")\n",
    "face_token4 = four[\"faces\"][0][\"face_token\"]\n",
    "face_token4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "79ec62ba",
   "metadata": {},
   "outputs": [],
   "source": [
    "import compare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "7044cf19",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1649011441,f911a5a6-3f2a-42f1-8fdc-989c7225b646',\n",
       " 'time_used': 319,\n",
       " 'confidence': 53.252,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "compare = compare.face_compare(API_key,API_secret,face_token3,face_token4)\n",
    "compare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "2ffa4aa6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "不是同一个人\n"
     ]
    }
   ],
   "source": [
    "# 最后判断是否为本宿舍成员\n",
    "if compare['confidence'] >= compare['thresholds']['1e-5']:\n",
    "    print(\"是同一个人\")\n",
    "else:\n",
    "    print(\"不是同一个人\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b73f0fae",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
